#include "page1_widget.h"
#include "welcome_widget.h"

Page1_Widget::Page1_Widget(welcome_Widget *widget)
{
    this->welcomewidget = widget;
    welcome_ui = this->welcomewidget->ui;

    update_flag = false;

    update_timer = new QTimer();

    if(update_timer != nullptr)
    {
        connect(update_timer,SIGNAL(timeout()),this,SLOT(updatetimer_timeout_handler()));
        update_timer->start(10);
    }

    //按钮槽函数
    connect(welcome_ui->pb_clear_rx_data,SIGNAL(clicked()),this,SLOT(on_pb_clear_rx_data_clicked()));     //建立信号槽
    connect(welcome_ui->pb_light_on,SIGNAL(clicked()),this,SLOT(on_pb_light_on_clicked()));
    connect(welcome_ui->pb_light_off,SIGNAL(clicked()),this,SLOT(on_pb_light_off_clicked()));
    connect(welcome_ui->pb_fan_on,SIGNAL(clicked()),this,SLOT(on_pb_fan_on_clicked()));
    connect(welcome_ui->pb_fan_off,SIGNAL(clicked()),this,SLOT(on_pb_fan_off_clicked()));

    nearlink_module = new bearpi_h3863("NONE");

    if(nearlink_module != nullptr)
    {
        read_nearlink_data_thread();
    }

    dashBoardWidget = new DashBoardWidget(this);
    dashBoardWidget->setUnit("%");
    dashBoardWidget->setText("电池电量");

    QHBoxLayout *layout = new QHBoxLayout(welcome_ui->gauge_container);
    layout->addWidget(dashBoardWidget);

    welcome_ui->gauge_container->resize(250,250);
}

Page1_Widget::~Page1_Widget()
{
    this->welcomewidget = nullptr;
    this->welcome_ui = nullptr;

}

//定时器超时处理
void Page1_Widget::updatetimer_timeout_handler()
{
    static unsigned int time_update_count = 0;
    
    std::string data;
    QString data_qstr;

    if(++time_update_count > 10)  //100 * 10ms
    {
        //qDebug() << ">>> updatetimer_timeout_handler >>>";
        time_update_count = 0;

        if(update_flag)
        {
            update_flag = false;

            rx_data_display(">>>>>>######>>>>>>");
            rx_data_display(QString("温度：")+QString::number(static_cast<int>(device_info.temperature)));
            rx_data_display(QString("湿度：")+QString::number(static_cast<int>(device_info.himidity)));
            rx_data_display(QString("亮度：")+QString::number(static_cast<int>(device_info.bright_value)));
            rx_data_display(QString("酸碱度：")+QString::number(static_cast<int>(device_info.ph_value)));
            rx_data_display(QString("电池电量：")+QString::number(static_cast<int>(device_info.battery_percentage)));

            rx_data_display("                  ");

            memcpy(&device_info_old, &device_info, sizeof(device_info_t));

            if(dashBoardWidget != nullptr)dashBoardWidget->setValue(static_cast<double>(device_info.battery_percentage));

            display_light_state(device_info.light_ctrl);
            display_fan_state(device_info.motor_ctrl);
        }
    }
}



void Page1_Widget::display_light_state(unsigned char state)
{
    QString light_state;

    if(state)light_state = QString(":/picture/light_on");
    else QString(":/picture/light_off");

    QPixmap pixmap(light_state);  // 替换为你的PNG图片路径
    welcome_ui->label_light_state->setPixmap(pixmap);
    welcome_ui->label_light_state->show();
}


void Page1_Widget::display_fan_state(unsigned char state)
{
    QString fan_state;

    if(state)fan_state = QString(":/picture/fan_on");
    else QString(":/picture/fan_off");

    QPixmap pixmap(fan_state);  // 替换为你的PNG图片路径
    welcome_ui->label_fan_state->setPixmap(pixmap);
    welcome_ui->label_fan_state->show();
}



void Page1_Widget::iot_kit_data_process(unsigned char *p_data, int len)
{
    static cmd_frame_t frame;

    memcpy(&frame, p_data, static_cast<size_t>(len));
    if(frame.frame_head != 0xF5)return;

    switch (frame.cmd)
    {
        case CMD_LIGHT_CTRL:
        {

        }break;

        case CMD_FAN_CTRL:
        {

        }break;

        case CMD_UPLOAD_DATA:
        {
            //qDebug() << ">>> CMD_UPLOAD_DATA >>>";
            memcpy(&device_info, frame.payload, sizeof(device_info_t));

            if(memcmp(&device_info, &device_info_old, sizeof(device_info_t)) != 0)update_flag = true;
        }break;

        default:break;
    }

}

void Page1_Widget::read_nearlink_data_thread(void)
{
    //读取星闪数据的线程
    QtConcurrent::run([this]()
    {
        bool running = true;
        static int count = 0;
        unsigned char data_buff[128];
        int read_bytes = 0;

        while(running)
        {
            read_bytes = nearlink_module->transmode_read_bytes_pool(data_buff, 128);

            if(read_bytes > 0)
            {
                iot_kit_data_process(data_buff, read_bytes);

#if 1
                count += read_bytes;

                std::cout << "all data size : " << count << " ";
                //处理这些数据
                std::cout << "proccessing data: ";
                for(int i = 0; i < read_bytes; i++)
                {
                    std::cout << std::hex << static_cast<int>(data_buff[i]) << " ";
                }
                std::cout << std::endl;
#endif
                read_bytes = 0;
            }
            //std::this_thread::sleep_for(std::chrono::milliseconds(1));
        }
    });

}




void Page1_Widget::on_pb_light_on_clicked()
{
    qDebug() << ">>> on_pb_light_on_clicked >>>";

    cmd_frame_t frame;
    unsigned char crc_value = 0;

    frame.frame_head = 0xF5;
    frame.cmd = CMD_LIGHT_CTRL;
    frame.length = 0x01;
    frame.payload[0] = 0x01;

    crc_value = static_cast<unsigned char>(frame.cmd + frame.length + frame.payload[0]);

    frame.payload[1] = crc_value;

    nearlink_module->transmode_send_bytes_data(reinterpret_cast<unsigned char*>(&frame), 5);
}

void Page1_Widget::on_pb_light_off_clicked()
{
    qDebug() << ">>> on_pb_light_off_clicked >>>";

    cmd_frame_t frame;
    unsigned char crc_value = 0;

    frame.frame_head = 0xF5;
    frame.cmd = CMD_LIGHT_CTRL;
    frame.length = 0x01;
    frame.payload[0] = 0x00;

    crc_value = static_cast<unsigned char>(frame.cmd + frame.length + frame.payload[0]);

    frame.payload[1] = crc_value;

    nearlink_module->transmode_send_bytes_data(reinterpret_cast<unsigned char*>(&frame), 5);
}

void Page1_Widget::on_pb_fan_on_clicked()
{
    qDebug() << ">>> on_pb_fan_on_clicked >>>";

    cmd_frame_t frame;
    unsigned char crc_value = 0;

    frame.frame_head = 0xF5;
    frame.cmd = CMD_FAN_CTRL;
    frame.length = 0x01;
    frame.payload[0] = 0x01;

    crc_value = static_cast<unsigned char>(frame.cmd + frame.length + frame.payload[0]);

    frame.payload[1] = crc_value;

    nearlink_module->transmode_send_bytes_data(reinterpret_cast<unsigned char*>(&frame), 5);
}

void Page1_Widget::on_pb_fan_off_clicked()
{
    qDebug() << ">>> on_pb_fan_off_clicked >>>";

    cmd_frame_t frame;
    unsigned char crc_value = 0;

    frame.frame_head = 0xF5;
    frame.cmd = CMD_FAN_CTRL;
    frame.length = 0x01;
    frame.payload[0] = 0x00;

    crc_value = static_cast<unsigned char>(frame.cmd + frame.length + frame.payload[0]);

    frame.payload[1] = crc_value;

    nearlink_module->transmode_send_bytes_data(reinterpret_cast<unsigned char*>(&frame), 5);
}


void Page1_Widget::on_pb_clear_rx_data_clicked()
{
    welcome_ui->plainTextEdit_rxdata_disp->clear();
}

void Page1_Widget::rx_data_display(QString str)
{
    welcome_ui->plainTextEdit_rxdata_disp->appendPlainText(str);
}

